package com.lixiaoyi.ems.service.impl;

import com.lixiaoyi.ems.dao.IEmpDao;
import com.lixiaoyi.ems.dao.impl.EmpDaoImpl;
import com.lixiaoyi.ems.entity.Emp;
import com.lixiaoyi.ems.entity.Page;
import com.lixiaoyi.ems.service.IEmpService;
import com.lixiaoyi.ems.utils.DbUtils;

import java.util.List;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-18
 */

public class EmpServiceImpl implements IEmpService {

    //持有dao层对象 方法
    private IEmpDao empDao = new EmpDaoImpl();

    @Override
    public List<Emp> showAllEmpByPage(Page page) {
        List<Emp> emps = null;
        try {
            DbUtils.begin();
            //赋值总条数 计算总页数
            long count = empDao.selectCount();
            page.setTotalPages((int)count);

            emps =  empDao.selectAll(page);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return emps;
    }

    @Override
    public int deldete(int id) {
        int result = 0;
        try {
            DbUtils.begin();
            result = empDao.delete(id);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int addEmp(Emp emp) {
        int result = 0;
        try {
            DbUtils.begin();
            result = empDao.insert(emp);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public Emp selectEmpById(int id) {
        Emp emp = null;
        try {
            DbUtils.begin();
            emp = empDao.select(id);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return emp;
    }

    @Override
    public int modifyEmp(Emp emp) {
        int result = 0;
        try {
            DbUtils.begin();
            result = empDao.update(emp);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }
}
